﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using OpenExpressApp.Editors;
using System.Windows.Data;
using Csla.Wpf;
using System.Windows;
using Itenso.Windows.Input;
using OpenExpressApp.MetaModel;

namespace OpenExpressApp.Module.WPF.Editors
{
    /// <summary>
    /// 文件类型的属性编辑器
    /// </summary>
    public class FileDataPropertyEditor : WPFPropertyEditor
    {
        private TextBox _tb;
        private DockPanel _memo;

        public FileDataPropertyEditor(BusinessObjectPropertyInfo propertyInfo, IObjectView view)
            : base(propertyInfo, view) { }

        /// <summary>
        /// 编辑器里面的文件数据
        /// </summary>
        public IFileData FileData
        {
            get
            {
                var name = PropertyInfo.LookupAttribute.LookupPropertyName;
                if (null == View.CurrentObject)
                {
                    return null;
                }
                else
                {
                    return View.CurrentObject.GetPropertyValue(name) as IFileData;
                }
            }
        }

        protected override FrameworkElement CreateEditingElement()
        {
            _memo = new DockPanel();

            Button btnOpen = new Button() ;
            btnOpen.SetValue(DockPanel.DockProperty, Dock.Right);
            btnOpen.CommandParameter = this;
            ButtonCommand.SetCommand(btnOpen, CommandRepository.NewCommand(CommandNames.OpenFile));
            _memo.Children.Add(btnOpen);

            //Button btnSave = new Button();
            //btnSave.SetValue(DockPanel.DockProperty, Dock.Right);
            //btnSave.CommandParameter = this;
            //ButtonCommand.SetCommand(btnSave, CommandRepository.NewCommand(CommandNames.FileSaveAs));
            //_memo.Children.Add(btnSave);

            //Button btnClearFile = new Button();
            //btnClearFile.SetValue(DockPanel.DockProperty, Dock.Right);
            //btnClearFile.CommandParameter = this;
            //ButtonCommand.SetCommand(btnClearFile, CommandRepository.NewCommand(CommandNames.ClearFile));
            //_memo.Children.Add(btnClearFile);

            Button btnAdd = new Button();
            btnAdd.SetValue(DockPanel.DockProperty, Dock.Right);
            btnAdd.CommandParameter = this;
            ButtonCommand.SetCommand(btnAdd, CommandRepository.NewCommand(CommandNames.AddFile));
            _memo.Children.Add(btnAdd);

            _tb = new TextBox()
            {
                Name = PropertyInfo.Name,
                IsReadOnly = true,
            };
            //绑定TextBox到对象属性
            Binding tbbinging = new Binding();
            this.PrepareBinding(tbbinging);
            _tb.SetBinding(TextBox.TextProperty, tbbinging);
            _memo.Children.Add(_tb);
            
            return _memo;
        }
        protected override void PrepareBinding(Binding binding)
        {
            base.PrepareBinding(binding);

            var lookupAttri = PropertyInfo.LookupAttribute;
            var lookupTypeInfo = ApplicationModel.GetBusinessObjectInfo(lookupAttri.LookupType);
            string path = lookupAttri.LookupPropertyName + "." + lookupTypeInfo.TitleInfo.Name;

            binding.Path = new PropertyPath(path);
        }
    }
}

